package com.tgy.crud.config;

import com.tgy.crud.converter.String2User;
import com.tgy.crud.interceptor.WebMvcInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.FormatterRegistry;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private String[] staticExcludePathPatterns = {};

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/tologin").setViewName("login");
    }


    @Bean
    public LocaleResolver localeResolver(){

        return  new AcceptHeaderLocaleResolver(){

            @Override
            public Locale resolveLocale(HttpServletRequest request) {

                String lang = request.getParameter("lang");

                if (StringUtils.isEmpty(lang)){

                    return super.resolveLocale(request);
                }

                String[] languages = lang.split("_");

                if (languages.length != 2){

                    return super.resolveLocale(request);
                }

                Locale locale = new Locale(languages[0], languages[1]);
                return locale;
            }
        };
    }

//    @Bean
//    public BeanPostProcessor beanPostProcessor(){
//
//        return new BeanPostProcessor() {
//            @Override
//            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//
//                log.info(beanName);
//                return bean;
//            }
//        };
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

//        registry.addInterceptor(new WebMvcInterceptor())
//                .addPathPatterns("/**")
//                .excludePathPatterns("/tologin","/login","/error/**","/favicon.ico","/css/**","/images/**","/js/**","/pug/**","/scss/**","/vendor/**");
        registry.addInterceptor(new WebMvcInterceptor(redisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns("/tologin","/login","/error/**","/test/**","/person/**","/favicon.ico","/css/**","/images/**","/js/**","/pug/**","/scss/**","/vendor/**");
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {

        /**
         * 添加类型转换器
         */
        registry.addConverter(new String2User());
    }
}
